package com.ruoyi.recruit.service.impl;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.recruit.domain.RecruitFile;
import com.ruoyi.recruit.domain.RecruitResult;
import com.ruoyi.recruit.domain.Recruiter;
import com.ruoyi.recruit.domain.vo.RecruiterVo;
import com.ruoyi.recruit.mapper.RecruiterMapper;
import com.ruoyi.recruit.service.IRecruiterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author MMDZ
 * @date 2022/10/24 11:59
 * @description TODO
 */
@Service
public class RecruiterServiceImpl implements IRecruiterService {

    @Autowired
    private RecruiterMapper recruiterMapper;

    @Override
    public List<RecruiterVo> selectList(Recruiter recruiter) {
        return recruiterMapper.selectList(recruiter);
    }

    @Override
    public Recruiter selectById(Long recruitId) {
        return recruiterMapper.selectById(recruitId);
    }

    @Override
    public RecruitResult selectResultById(Long recruitResultId) {
        return recruiterMapper.selectResultById(recruitResultId);
    }

    @Override
    public int insertRecruiter(Recruiter recruiter) {
        return recruiterMapper.insertRecruiter(recruiter);
    }

    @Override
    public int updateRecruiter(Recruiter recruiter) {
        return recruiterMapper.updateRecruiter(recruiter);
    }

    @Override
    public int insertRecruitResult(RecruitResult recruitResult) {
        return recruiterMapper.insertRecruitResult(recruitResult);
    }

    @Override
    public int updateRecruitResult(RecruitResult recruitResult) {
        return recruiterMapper.updateRecruitResult(recruitResult);
    }

    @Transactional
    @Override
    public AjaxResult fileUpload(MultipartFile file, Long recruitId) throws IOException {
        if (!file.isEmpty()) {
            //获得文件原名 如：abc.txt
            String fileFullName = file.getOriginalFilename();
            String fileName = fileFullName.substring(0, fileFullName.lastIndexOf("."));

            //文件名不能带特殊字符（下载时特殊字符会导致下载失败）
            if(fileName.contains("&") || fileName.contains("?"))
                return AjaxResult.error("上传资料失败，资料名称含有特殊字符");

            //文件大小
            double size = file.getSize();
            //文件类型
            String type = fileFullName.substring(fileFullName.lastIndexOf(".") + 1).toLowerCase();

            RecruitFile recruitFile = new RecruitFile();
            recruitFile.setRecruitId(recruitId);
            recruitFile.setFileName(fileName);
            recruitFile.setSize(size / 1024);//（单位：KB）
            recruitFile.setType(type);
            recruitFile.setCreateBy(SecurityUtils.getNickname());

            String recruitUrl = FileUploadUtils.upload(RuoYiConfig.getRecruitPath(), file, UserConstants.EXTRACT_FILE_NAME_RECRUIT);
            recruitFile.setUrl(recruitUrl);

            int rows = insertRecruitFile(recruitFile);
            return rows > 0 ? AjaxResult.success("上传成功", recruitFile) : AjaxResult.error("上传失败");
        }

        return AjaxResult.error("上传资料异常，请联系管理员");
    }

    @Override
    public List<RecruitFile> selectFileList(Long recruitId) {
        return recruiterMapper.selectFileList(recruitId);
    }

    @Override
    public int insertRecruitFile(RecruitFile recruitFile) {
        return recruiterMapper.insertRecruitFile(recruitFile);
    }

    @Override
    public int deleteRecruitFile(Long recruitFileId) {
        return recruiterMapper.deleteRecruitFile(recruitFileId);
    }
}
